package com.espressif.iot.ui.device;

import com.tigerfire.iot.R;
import com.espressif.iot.base.application.EspApplication;
import com.espressif.iot.device.IEspDevice;
import com.espressif.iot.device.array.IEspDeviceArray;
import com.espressif.iot.type.device.IEspDeviceStatus;
import com.espressif.iot.user.IEspUser;
import com.espressif.iot.user.builder.BEspUser;
import com.espressif.iot.util.EspStrings;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnDismissListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class DevicePlugsTriggers extends Activity{

	protected IEspUser mUser;
	private EspApplication espapplicationinstance;
	protected IEspDevice mIEspDevice;
	
	private String deviceKey = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
//		mUser = BEspUser.getBuilder().getInstance();
//        espapplicationinstance = EspApplication.sharedInstance();
//        mIEspDevice = espapplicationinstance.getplugs();
		
		setContentView(R.layout.jttc_pluglog);
		Intent intent = getIntent();
        deviceKey = intent.getStringExtra(EspStrings.Key.DEVICE_KEY_KEY);
        
		new DeviceTask(this).execute();
		
	}
	private Handler myhandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			Intent fintent = new Intent();
			fintent.putExtra(EspStrings.Key.DEVICE_KEY_KEY, deviceKey);
	            
			fintent.setClass(getApplicationContext(), DevicePlugsActivity.class);
			startActivity(fintent);
			finish();
		}
		
	};
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}
	
	
	 private class DeviceTask extends AsyncTask<IEspDeviceStatus, Void, Boolean> implements OnDismissListener
	    {
	        private Activity mActivity;
	        
	        private ProgressDialog mDialog;
	        
	        private int mCommand;
	        
	        public DeviceTask(Activity activity)
	        {
	            mActivity = activity;
	        }
	        
	        @Override
	        protected void onPreExecute()
	        {
	           /// executePrepare();
	            
	            //showDialog();
	        }
	        
	        @Override
	        protected Boolean doInBackground(IEspDeviceStatus... params)
	        {
	         
	             try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	          return true;
	        }
	        
	        @Override
	        protected void onPostExecute(Boolean result)
	        {
	            //log.debug("DeviceTask result = " + result);
	            //releaseDialog();
	            // executeFinish(mCommand, result);
	        	myhandler.sendEmptyMessage(0);
	        }
	        
	        private void showDialog()
	        {
	            mDialog = new ProgressDialog(mActivity);
	            mDialog.setMessage(getString(R.string.esp_device_task_dialog_message));
	            mDialog.setCanceledOnTouchOutside(false);
	            mDialog.setOnDismissListener(this);
	            mDialog.show();
	        }
	        
	        private void releaseDialog()
	        {
	            if (mDialog != null)
	            {
	                mDialog.dismiss();
	                mDialog = null;
	            }
	        }
	        
	        @Override
	        public void onDismiss(DialogInterface dialog)
	        {
	            cancel(true);
	            mDialog = null;
	        }
	    }
}
